package com.mhy.netsafe.utils;

import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.animation.BounceInterpolator;
import android.widget.TextView;
import android.widget.Toast;

import com.mhy.netsafe.MyApplication;
import com.mhy.netsafe.R;
import androidx.core.content.ContextCompat;

public class MToast extends Toast {
    @SuppressLint("StaticFieldLeak")
    private static MToast toast;
    @SuppressLint("StaticFieldLeak")
    private static TextView toast_tv;

    private MToast(Context context) {
        super(context);
    }
    /**
     * cancal
     */
    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
//            toast=null;
        }
    }

    private static void initToast(Context context, CharSequence text) {
        try {
            cancelToast();
            toast = new MToast(context);
            toast_tv=new TextView(context);
            toast_tv.setBackground(ContextCompat.getDrawable(context, R.mipmap.toast_frame));
            toast_tv.setTextColor(ContextCompat.getColor(context, android.R.color.white));
            toast_tv.setGravity(Gravity.CENTER);
            //toast.setGravity(17, 0, 0);//位置
            toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER, 0, 150);
            toast_tv.setText(text);
            toast.setView(toast_tv);
        }
        catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static void showToast(Context context, CharSequence charSequence) {
        if (charSequence != null) {
            initToast(context,charSequence);
            toast.setDuration(Toast.LENGTH_LONG);
            toast_tv.setPivotY(0.0f);
            toast_tv.setScaleY(0.0f);
           final ObjectAnimator ofFloat = ObjectAnimator.ofFloat(toast_tv, "scaleY", 0.0f, 1.0f);
            ofFloat.setStartDelay(200L);
            ofFloat.setDuration(800L);//时长
            ofFloat.setInterpolator(new BounceInterpolator());
            ofFloat.start();
            toast.show();
        }
    }


    @Override
    public void cancel() {
            super.cancel();
    }

    @Override
    public void show() {
            super.show();
    }
}


